﻿using System;
using System.IO;

namespace StudentManagementSystem
{
    class Program
    {
        static void Main(string[] args)
        {
            string dbPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StudentManagementSystem.db");
            var dbManager = new DatabaseManager(dbPath);

            while (true)
            {
                Console.WriteLine("\n===== 学生管理系统 =====");
                Console.WriteLine("1. 添加学校");
                Console.WriteLine("2. 添加班级");
                Console.WriteLine("3. 添加学生");
                Console.WriteLine("4. 查看学校列表");
                Console.WriteLine("5. 查看班级列表");
                Console.WriteLine("6. 查看学生列表");
                Console.WriteLine("7. 查看操作日志");
                Console.WriteLine("0. 退出系统");
                Console.Write("请选择操作：");

                string choice = Console.ReadLine();

                switch (choice)
                {
                    case "1":
                        AddSchool(dbManager);
                        break;
                    case "2":
                        AddClassroom(dbManager);
                        break;
                    case "3":
                        AddStudent(dbManager);
                        break;
                    case "4":
                        DisplaySchools(dbManager);
                        break;
                    case "5":
                        DisplayClassrooms(dbManager);
                        break;
                    case "6":
                        DisplayStudents(dbManager);
                        break;
                    case "7":
                        DisplayLogs(dbManager);
                        break;
                    case "0":
                        return;
                    default:
                        Console.WriteLine("无效的选择，请重新输入。");
                        break;
                }
            }
        }

        static void AddSchool(DatabaseManager dbManager)
        {
            Console.Write("请输入学校名称：");
            string name = Console.ReadLine();
            Console.Write("请输入学校地址：");
            string address = Console.ReadLine();

            var school = new School { Name = name, Address = address };
            dbManager.AddSchool(school);
            Console.WriteLine("学校添加成功！");
        }

        static void AddClassroom(DatabaseManager dbManager)
        {
            DisplaySchools(dbManager);
            Console.Write("请输入班级名称：");
            string name = Console.ReadLine();
            Console.Write("请选择所属学校ID：");
            if (int.TryParse(Console.ReadLine(), out int schoolId))
            {
                var classroom = new Classroom { Name = name, SchoolId = schoolId };
                dbManager.AddClassroom(classroom);
                Console.WriteLine("班级添加成功！");
            }
            else
            {
                Console.WriteLine("无效的学校ID！");
            }
        }

        static void AddStudent(DatabaseManager dbManager)
        {
            DisplayClassrooms(dbManager);
            Console.Write("请输入学生姓名：");
            string name = Console.ReadLine();
            Console.Write("请输入学生年龄：");
            if (int.TryParse(Console.ReadLine(), out int age))
            {
                Console.Write("请选择所属班级ID：");
                if (int.TryParse(Console.ReadLine(), out int classroomId))
                {
                    var student = new Student { Name = name, Age = age, ClassroomId = classroomId };
                    dbManager.AddStudent(student);
                    Console.WriteLine("学生添加成功！");
                }
                else
                {
                    Console.WriteLine("无效的班级ID！");
                }
            }
            else
            {
                Console.WriteLine("无效的年龄！");
            }
        }

        static void DisplaySchools(DatabaseManager dbManager)
        {
            var schools = dbManager.GetSchools();
            Console.WriteLine("\n学校列表：");
            foreach (var s in schools)
            {
                Console.WriteLine($"学校ID: {s.Id}, 名称: {s.Name}, 地址: {s.Address}");
            }
        }

        static void DisplayClassrooms(DatabaseManager dbManager)
        {
            var classrooms = dbManager.GetClassrooms();
            Console.WriteLine("\n班级列表：");
            foreach (var c in classrooms)
            {
                Console.WriteLine($"班级ID: {c.Id}, 名称: {c.Name}, 所属学校ID: {c.SchoolId}");
            }
        }

        static void DisplayStudents(DatabaseManager dbManager)
        {
            var students = dbManager.GetStudents();
            Console.WriteLine("\n学生列表：");
            foreach (var s in students)
            {
                Console.WriteLine($"学生ID: {s.Id}, 姓名: {s.Name}, 年龄: {s.Age}, 班级ID: {s.ClassroomId}");
            }
        }

        static void DisplayLogs(DatabaseManager dbManager)
        {
            var logs = dbManager.GetLogs();
            Console.WriteLine("\n操作日志：");
            foreach (var log in logs)
            {
                Console.WriteLine($"操作: {log.Operation}, 表: {log.TableName}, 记录ID: {log.RecordId}, 时间: {log.Timestamp}");
            }
        }
    }
}
